package com.example;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;

public class CodeGenerator {
    public static void main(String[] args) {
        //获取代码生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();

        //设置数据库相关配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        autoGenerator.setDataSource(dataSourceConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus-generator2/src/main/java"); //代码生成位置
        globalConfig.setOpen(false);  //生成后是否打开代码所在目录
        globalConfig.setAuthor("Anomaly");  //作者
        globalConfig.setFileOverride(false);  //是否覆盖原始文件
        globalConfig.setMapperName("%sDao");  //数据层接口名，%s是占位符
        globalConfig.setIdType(IdType.AUTO);  //id生成策略
        autoGenerator.setGlobalConfig(globalConfig);

        //包名相关配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.example");  //包名
        packageConfig.setEntity("domain");  //实体类包名
        packageConfig.setMapper("dao");  //数据层包名
        autoGenerator.setPackageInfo(packageConfig);

        //策略设置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("user");  //参与生成的表名
        //strategyConfig.setTablePrefix("tbl_");  //数据表名前缀
        strategyConfig.setRestControllerStyle(true);  //启用REST风格
        //strategyConfig.setVersionFieldName("version");  //乐观锁字段名
        //strategyConfig.setLogicDeleteFieldName("deleted");  //逻辑删除字段名
        strategyConfig.setEntityLombokModel(true);  //启用Lombok
        autoGenerator.setStrategy(strategyConfig);

        //执行生成操作
        autoGenerator.execute();

    }
}
